package com.animee.health.bean;

import android.content.Context;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class FileUtils {


    public static void main(String[] args) {
        // List<String> foodList = FileUtils.readerFileFromVM("foods.txt");
        // for (String s : foodList) {
        //     System.out.println(s);
        // }
    }

    /**
     * 读取 src/main/resources/dataset 目录下的文件下, 并把字符串切割返回一个List集合
     *
     * @param fileName 文件的名称
     * @return
     */
    public static List<String> readerFileFromSrc(String fileName) {
        FileReader reader = null;
        BufferedReader bufferedReader = null;

        // /data/user/my_dataset
        List<String> list = new ArrayList<>();
        try {
            reader = new FileReader(new File("src\\main\\resources\\dataset\\" + fileName));
            // reader = new FileReader(new File("D:\\Code\\AndroidStudioProjects\\Healthy_diet\\app\\src\\main\\resources\\dataset\\" + fileName));
            // D:\Code\AndroidStudioProjects\Healthy_diet\app\src\main\resources\dataset\foods.txt
            bufferedReader = new BufferedReader(reader);

            String line = bufferedReader.readLine();
            while (line != null) {
                // System.out.println("line = " + line);
                // String[] split = line.split("\\|");
                // list.addAll(Arrays.asList(split));
                list.add(line);
                line = bufferedReader.readLine();
            }
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return list;
    }

    /**
     * 读取 src/main/resources/dataset 目录下的文件下, 并把字符串切割返回一个List集合
     *
     * @param fileName 文件的名称
     * @return
     */
    public static List<String> readerFileFromVM(Context context, String fileName) throws IOException {
        // File file;
        InputStreamReader inputReader = null;
        FileReader reader = null;
        BufferedReader bufferedReader = null;

        // /data/user/my_dataset
        // /data/user/my_dataset/foods.txt
        List<String> list = new ArrayList<>();
        try {
            // file = new File(Environment.getExternalStorageDirectory(), fileName);
            // reader = new FileReader(file);
            // reader = new FileReader(new File("/data/user/my_dataset/" + fileName));
            // reader = new FileReader(new File("D:\\Code\\AndroidStudioProjects\\Healthy_diet\\app\\src\\main\\resources\\dataset\\" + fileName));
            // D:\Code\AndroidStudioProjects\Healthy_diet\app\src\main\resources\dataset\foods.txt
            // inputReader = new InputStreamReader(context.getResources().getAssets().open(fileName));
            inputReader = new InputStreamReader(context.getResources().getAssets().open(fileName));
            bufferedReader = new BufferedReader(inputReader);

            String line = bufferedReader.readLine();
            while (line != null) {
                // System.out.println("line = " + line);
                // String[] split = line.split("\\|");
                // list.addAll(Arrays.asList(split));
                list.add(line);
                line = bufferedReader.readLine();
            }
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // if (reader != null) {
            //     try {
            //         reader.close();
            //     } catch (IOException e) {
            //         e.printStackTrace();
            //     }
            // }
            if (inputReader != null) {
                try {
                    inputReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return list;
    }
}
